/*******************************************************************************
 * Copyright (c) 2008 Smart Mai(smardio@gmail.com).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Smart Mai(smardio@gmail.com) - initial API and implementation
 ******************************************************************************/
package com.smartdio.company.views;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.contexts.IContextActivation;
import org.eclipse.ui.contexts.IContextService;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.help.IWorkbenchHelpSystem;
import org.eclipse.ui.part.ViewPart;

/**
 * @author smart
 * 
 */
public class CompanyView extends ViewPart {

	public static final String ID = "com.smartdio.company.views.CompanyView";

	public static CompanyView instance;

	private Composite top = null;
	private CompanyList companyList = null;

	/**
	 * 
	 */
	public CompanyView() {
		instance = this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
	 * .Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		top = new Composite(parent, SWT.NONE);
		top.setLayout(new FillLayout());
		createCompanyList();
		
		IContextService contextService = (IContextService) getSite()
		  .getService(IContextService.class);
		IContextActivation contextActivation = contextService.activateContext("com.smartdio.company.context");
		
		// 添加动态帮助
		IWorkbenchHelpSystem help = PlatformUI.getWorkbench().getHelpSystem();
		help.setHelp(parent, "com.smartdio.company.companyview");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {


	}
	
	

	/**
	 * This method initializes companyList
	 * 
	 */
	private void createCompanyList() {
		companyList = new CompanyList(top, SWT.NONE);
		
		TreeViewer viewer = companyList.getTreeViewer();
		viewer.setInput("");
		// This is new code
		// First we create a menu Manager
		
		MenuManager menuManager = new MenuManager();
		Menu menu = menuManager.createContextMenu(viewer.getTree());
		// Set the MenuManager
		viewer.getTree().setMenu(menu);
		getSite().registerContextMenu(menuManager, viewer);
		// Make the selection available
		getSite().setSelectionProvider(viewer);

		companyList.getTree().setMenu(menu);
		


	}

	/**
	 * 这个main方法仅用于测试
	 */
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		// 建立CompanyView的实例
		CompanyView view = new CompanyView();
		view.createPartControl(shell);
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();

	}

	
	/**
	 * 当数据被更新了，通过这个方法来通知CompanyView刷新显示
	 */
	public void refresh() {
		// TODO 还没有编写CompanyView的刷新方法
	}

	public CompanyList getCompanyList() {
		return companyList;
	}
	
	
}
